package admin

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"zhuandan/data_source"
	"zhuandan/models"
)

type ProcureloglistRBody struct {
	Data       []models.Procurelogsql `json:"data"`
	PageNo     int                    `json:"pageNo"`
	PageSize   int                    `json:"pageSize"`
	TotalCount int64                  `json:"totalCount"`
	TotalPage  int64                  `json:"totalPage"`
}

type ProcureloglistBody struct {
	Page     int    `json:"pageNo" from:"pageNo"`
	Pagesize int    `json:"pageSize" from:"pageSize"`
	Inttime  int64  `json:"inttime" from:"inttime"`
	Endtime  int64  `json:"endtime" from:"endtime"`
	Phone    string `json:"phone" from:"phone"`
	Nickname string `json:"nickname" from:"nickname"`
	Title    string `gorm:"column:title" json:"title"`
}

func Procureloglist(ctx *gin.Context) {
	request := ProcureloglistBody{}
	ctx.ShouldBind(&request)
	title := request.Title
	nickname := request.Nickname
	phone := request.Phone
	inttime := request.Inttime
	endtime := request.Endtime
	currentPage := request.Page
	pageSize := request.Pagesize
	offsetNum := pageSize * (currentPage - 1)
	var count int64
	//var users []models.Procure
	var users []models.Procurelogsql
	res := data_source.Db.Model(&models.Procurelog{}).Select("procurelog.*, user.nickname,  user.logo, user.phone").Joins("left join user on user.id = procurelog.uid")
	//res := data_source.Db
	if inttime > 0 {
		res = res.Where("procurelog.addtime >= ?", inttime)
	}
	if endtime > 0 {
		res = res.Where("procurelog.addtime < ?", endtime+86400)
	}
	if nickname != "" {
		res = res.Where("user.nickname = ?", nickname)
	}
	if phone != "" {
		res = res.Where("user.phone = ?", phone)
	}
	if title != "" {
		res = res.Where("procurelog.title = ?", title)
	}
	res.Model(&models.Procurelog{}).Count(&count)
	res.Limit(pageSize).Offset(offsetNum).Order("procurelog.id desc").Find(&users)

	var totalPage int64
	mo := count % int64(pageSize)
	if mo > 0 {
		totalPage = count/int64(pageSize) + 1
	} else {
		totalPage = count / int64(pageSize)
	}
	data := ProcureloglistRBody{
		Data:       users,
		PageNo:     currentPage,
		PageSize:   pageSize,
		TotalCount: count,
		TotalPage:  totalPage,
	}
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    data,
		"msg":     "ok",
	})
	return

}

type FabulogRBody struct {
	Data       []models.Fabulogsql `json:"data"`
	PageNo     int                 `json:"pageNo"`
	PageSize   int                 `json:"pageSize"`
	TotalCount int64               `json:"totalCount"`
	TotalPage  int64               `json:"totalPage"`
}

type FabulogBody struct {
	Page     int    `json:"pageNo" from:"pageNo"`
	Pagesize int    `json:"pageSize" from:"pageSize"`
	Inttime  int64  `json:"inttime" from:"inttime"`
	Endtime  int64  `json:"endtime" from:"endtime"`
	Phone    string `json:"phone" from:"phone"`
	Nickname string `json:"nickname" from:"nickname"`
	Title    string `gorm:"column:title" json:"title"`
}

func Fabulog(ctx *gin.Context) {
	request := ProcureloglistBody{}
	ctx.ShouldBind(&request)
	nickname := request.Nickname
	phone := request.Phone
	inttime := request.Inttime
	endtime := request.Endtime
	currentPage := request.Page
	pageSize := request.Pagesize
	offsetNum := pageSize * (currentPage - 1)
	var count int64
	//var users []models.Procure
	var users []models.Fabulogsql
	res := data_source.Db.Model(&models.Fabulog{}).Select("fabulog.*, user.nickname,  user.logo, user.phone").Joins("left join user on user.id = fabulog.uid")
	//res := data_source.Db
	if inttime > 0 {
		res = res.Where("fabulog.addtime >= ?", inttime)
	}
	if endtime > 0 {
		res = res.Where("fabulog.addtime < ?", endtime+86400)
	}
	if nickname != "" {
		res = res.Where("user.nickname = ?", nickname)
	}
	if phone != "" {
		res = res.Where("user.phone = ?", phone)
	}

	res.Model(&models.Fabulog{}).Count(&count)
	res.Limit(pageSize).Offset(offsetNum).Order("fabulog.id desc").Find(&users)

	var totalPage int64
	mo := count % int64(pageSize)
	if mo > 0 {
		totalPage = count/int64(pageSize) + 1
	} else {
		totalPage = count / int64(pageSize)
	}
	data := FabulogRBody{
		Data:       users,
		PageNo:     currentPage,
		PageSize:   pageSize,
		TotalCount: count,
		TotalPage:  totalPage,
	}
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    data,
		"msg":     "ok",
	})
	return

}
